package com.ruoyi.kiku.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.kiku.domain.model.ImageModel;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kiku.mapper.KikuPicMapper;
import com.ruoyi.kiku.domain.KikuPic;
import com.ruoyi.kiku.service.IKikuPicService;

/**
 * 小鞠图片信息Service业务层处理
 *
 * @author TT
 * @date 2025-03-31
 */
@Service
public class KikuPicServiceImpl implements IKikuPicService
{
    @Autowired
    private KikuPicMapper kikuPicMapper;
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询小鞠图片信息
     *
     * @param id 小鞠图片信息主键
     * @return 小鞠图片信息
     */
    @Override
    public KikuPic selectKikuPicById(Long id)
    {
        return kikuPicMapper.selectKikuPicById(id);
    }

    /**
     * 查询小鞠图片信息列表
     *
     * @param kikuPic 小鞠图片信息
     * @return 小鞠图片信息
     */
    @Override
    public List<KikuPic> selectKikuPicList(KikuPic kikuPic)
    {
        return kikuPicMapper.selectKikuPicList(kikuPic);
    }

    /**
     * 新增小鞠图片信息
     *
     * @param kikuPic 小鞠图片信息
     * @return 结果
     */
    @Override
    public int insertKikuPic(KikuPic kikuPic)
    {
        kikuPic.setCreateTime(DateUtils.getNowDate());
        return kikuPicMapper.insertKikuPic(kikuPic);
    }

    /**
     * 修改小鞠图片信息
     *
     * @param kikuPic 小鞠图片信息
     * @return 结果
     */
    @Override
    public int updateKikuPic(KikuPic kikuPic)
    {
        kikuPic.setUpdateTime(DateUtils.getNowDate());
        return kikuPicMapper.updateKikuPic(kikuPic);
    }

    /**
     * 批量删除小鞠图片信息
     *
     * @param ids 需要删除的小鞠图片信息主键
     * @return 结果
     */
    @Override
    public int deleteKikuPicByIds(Long[] ids)
    {
        return kikuPicMapper.deleteKikuPicByIds(ids);
    }

    /**
     * 删除小鞠图片信息信息
     *
     * @param id 小鞠图片信息主键
     * @return 结果
     */
    @Override
    public int deleteKikuPicById(Long id)
    {
        return kikuPicMapper.deleteKikuPicById(id);
    }

    @Override
    public ImageModel getCategoryImages() {
        List<KikuPic> kikuPics = kikuPicMapper.selectKikuPicList(null);
        // 预加载字典数据
        Map<String, String> picTypeDict = dictTypeService.selectDictDataByType("cecily_pic_type")
                .stream()
                .collect(Collectors.toMap(
                        d -> d.getDictValue(),
                        d -> d.getDictLabel()
                ));

        Map<String, String> sourceTypeDict = dictTypeService.selectDictDataByType("kiku_source_type")
                .stream()
                .collect(Collectors.toMap(
                        d -> d.getDictValue(),
                        d -> d.getDictLabel()
                ));

        // 按图片类型分组
        Map<Integer, List<KikuPic>> groupedByType = kikuPics.stream()
                .collect(Collectors.groupingBy(KikuPic::getPicType));

        ImageModel imageModel = new ImageModel();
        List<ImageModel.Category> categories = groupedByType.entrySet().stream()
                .map(entry -> {
                    ImageModel.Category category = new ImageModel.Category();
                    category.setName(picTypeDict.get(entry.getKey().toString()));

                    category.setImages(entry.getValue().stream()
                            .map(pic -> {
                                ImageModel.Image image = new ImageModel.Image();
                                image.setUrl(pic.getPicUrl());
                                image.setName(pic.getPicName());
                                image.setSource(sourceTypeDict.get(pic.getPicSource().toString()));
                                image.setSourceUrl(""); // 可根据需要设置
                                return image;
                            })
                            .collect(Collectors.toList()));

                    return category;
                })
                .collect(Collectors.toList());

        imageModel.setCategories(categories);
        return imageModel;
    }
}
